package com.tcwlkj.uuService.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.tcwlkj.uuService.NetWork.HttpIp;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.GoodEvaluteModel;
import com.tcwlkj.uuService.model.HonorBean;
import com.tcwlkj.uuService.widget.CircularImage;

import java.util.ArrayList;
import java.util.List;

import static com.tcwlkj.uuService.fragment.HonorsFragment.GOODS_TYPE;
import static com.tcwlkj.uuService.fragment.HonorsFragment.INCOME_TYPE;


/**
 * Created by ${tanbo} on 2017/8/4 0004.
 */

public class HonorAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    /**
     * ItemClick的回调接口
     */
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(HonorAdapter.OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    private List<GoodEvaluteModel> mData =new ArrayList<GoodEvaluteModel>();
    private Context context;
    private String type;
    private String time;

    public enum Item_Type {
        RECYCLEVIEW_ITEM_TYPE_1,
        RECYCLEVIEW_ITEM_TYPE_2,
    }

    public HonorAdapter(Context context, List<GoodEvaluteModel> datas, String type,String time) {

        this.mData=datas;
        this.context = context;
        this.type=type;
        this.time=time;
    }

    public void updateData(List<GoodEvaluteModel> data,String type,String time) {
        this.mData = data;
        this.type=type;
        this.time=time;
        notifyDataSetChanged();
    }
    /**
     * 创建ViewHolder
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == Item_Type.RECYCLEVIEW_ITEM_TYPE_1.ordinal()) {
            View mView = LayoutInflater.from(context).inflate(R.layout.item_honor_first,parent, false);
            ViewHolderFirst viewHolder = new ViewHolderFirst(mView);
            return viewHolder;
        } else if (viewType == Item_Type.RECYCLEVIEW_ITEM_TYPE_2.ordinal()) {
            View mView = LayoutInflater.from(context).inflate(R.layout.item_recyclerview_honor,parent, false);
            ViewHolderOther viewHolder = new ViewHolderOther(mView);
            return viewHolder;
        }

        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        GoodEvaluteModel dataBean=mData.get(position);
        if (holder instanceof ViewHolderFirst) {
            Glide.with(context).load(dataBean.getBigPhoto()).into( ((ViewHolderFirst) holder).iv_honor_first_head);

            ((ViewHolderFirst) holder).pai_ming_first_pic.setVisibility(View.VISIBLE);

            ((ViewHolderFirst) holder).tv_first_honor_name.setText(dataBean.getRealName());
            ((ViewHolderFirst) holder).tv_first_honor_num.setText("工号： "+dataBean.getUserCode());
            if(type.equals(GOODS_TYPE)){
                if(time.equals("7")){
                    ((ViewHolderFirst) holder).tv_first_honor_desc.setText("本周冠军"+dataBean.getRealName()+"累计获得"+Integer.parseInt(dataBean.getCount())+"个好评");
                }else {
                    ((ViewHolderFirst) holder).tv_first_honor_desc.setText("本月冠军"+dataBean.getRealName()+"累计获得"+Integer.parseInt(dataBean.getCount())+"个好评");
                }
            }else if(type.equals(INCOME_TYPE)){
                ((ViewHolderFirst) holder).tv_first_honor_desc.setText("累计收入"+dataBean.getMoney()+"元");
            }
        } else if (holder instanceof ViewHolderOther) {
            Glide.with(context).load(dataBean.getBigPhoto()).into( ((ViewHolderOther) holder).iv_honor_new_head);
            if(position==1){
                ((ViewHolderOther) holder).pai_ming_pic_mingci.setVisibility(View.VISIBLE);
                ((ViewHolderOther) holder).pai_ming_pic_mingci.setImageResource(R.mipmap.paiming_2);
            }else if(position==2){
                ((ViewHolderOther) holder).pai_ming_pic_mingci.setVisibility(View.VISIBLE);
                ((ViewHolderOther) holder).pai_ming_pic_mingci.setImageResource(R.mipmap.paiming_3);
            }else{
                ((ViewHolderOther) holder).pai_ming_pic_mingci.setVisibility(View.GONE);
            }
            ((ViewHolderOther) holder).honor_name_new.setText(dataBean.getRealName());
            ((ViewHolderOther) holder).honor_person_num.setText("工号： "+dataBean.getUserCode());
          if(type.equals(GOODS_TYPE)){
              ((ViewHolderOther) holder).honor_count.setText(String.valueOf(Integer.parseInt(dataBean.getCount())));
              ((ViewHolderOther) holder).honor_danwei.setText("个");
          }else if (type.equals(INCOME_TYPE)){
              ((ViewHolderOther) holder).honor_count.setText(dataBean.getMoney());
              ((ViewHolderOther) holder).honor_danwei.setText("元");
          }
        }
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                    notifyDataSetChanged();
                }
            });
        }
    }
    //返回值赋值给onCreateViewHolder的参数 viewType
    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return Item_Type.RECYCLEVIEW_ITEM_TYPE_1.ordinal();
        } else if (position>0) {
            return Item_Type.RECYCLEVIEW_ITEM_TYPE_2.ordinal();
        }
        return -1;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolderFirst extends RecyclerView.ViewHolder {
        public CircularImage iv_honor_first_head;
        public ImageView pai_ming_first_pic;
        public TextView tv_first_honor_name;
        public TextView tv_first_honor_num;
        public TextView tv_first_honor_desc;
        public ViewHolderFirst(View itemView) {
            super(itemView);
            iv_honor_first_head = (CircularImage) itemView.findViewById(R.id.iv_honor_first_head);
            pai_ming_first_pic = (ImageView) itemView.findViewById(R.id.pai_ming_first_pic);
            tv_first_honor_name = (TextView) itemView.findViewById(R.id.tv_first_honor_name);
            tv_first_honor_num = (TextView) itemView.findViewById(R.id.tv_first_honor_num);
            tv_first_honor_desc = (TextView) itemView.findViewById(R.id.tv_first_honor_desc);
        }
    }
    class ViewHolderOther extends RecyclerView.ViewHolder {
        public CircularImage iv_honor_new_head;
        public ImageView pai_ming_pic_mingci;
        public TextView honor_name_new;
        public TextView honor_person_num;
        public TextView honor_count;
        public TextView honor_danwei;

        public ViewHolderOther(View itemView) {
            super(itemView);
            iv_honor_new_head = (CircularImage) itemView.findViewById(R.id.iv_honor_new_head);
            pai_ming_pic_mingci = (ImageView) itemView.findViewById(R.id.pai_ming_pic_mingci);
            honor_name_new = (TextView) itemView.findViewById(R.id.honor_name_new);
            honor_person_num = (TextView) itemView.findViewById(R.id.honor_person_num);
            honor_count = (TextView) itemView.findViewById(R.id.honor_count);
            honor_danwei = (TextView) itemView.findViewById(R.id.honor_danwei);
        }
    }
    private static long lastClickTime = 0L;

    private boolean isShortTime(double time1,double time2){
       if(time1-time2>120000){
           return true;
       }else{
           return false;
       }
    }


    public void changeType(String type){
        this.type=type;
        notifyDataSetChanged();
    }
}
